package org.jfk.core.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jfk.core.model.SqlFilter;
import org.jfk.core.utils.JsonUtil;
import org.jfk.sys.user.User;

import com.jfinal.core.Controller;

/**
 * 所有controller继承此类
 * @author jxf
 * 2014年12月21日
 */
public class BaseController extends Controller {
	
	public final static String PAGE_SIZE = "pageSize";
	public final static String CURRENT_PAGE = "currentPage";
	
	public int currentPage = 1;
	public int limit = 10;
	public String sqlSort = "";
	
	
	protected String[][] listColumns;
	protected List<Object> param = new ArrayList<Object>();
	
	public Map resultJson = null;
	
	/**
	 * 通过list的request参数limit和offset进行分页处理。
	 */
	public void addPageAttr(){
		String order = getPara("order");
		String sort = getPara("sort");
		int defaultLimit = 10;
		if (getPara("dlgFlag") != null){
			defaultLimit = 5;
		}
		limit = getParaToInt("limit",defaultLimit);
		int offset = getParaToInt("offset",0);
		
		currentPage = offset/limit + 1;
		
		if(sort!=null&&!"".equals(sort.trim())){
			sqlSort = sqlSort + "order by " + sort + " " + order;
		}
	}


	public SqlFilter genJsonFilter(){
		SqlFilter filter = new SqlFilter();
		String jsonfilter = getPara("filter");
		Map mapFilter = JsonUtil.jsonToMap(jsonfilter);
		if (mapFilter != null && mapFilter.size() > 0) {
			Iterator itr = mapFilter.keySet().iterator();
			while (itr.hasNext()) {
				Object key = itr.next();
				Map qry = (Map)mapFilter.get(key);
				String op = qry.get("op").toString();
				String value = qry.get("value").toString();
				String attrKey = key.toString();
				//String sKey = attrKey.replace("_", ".");
				String sKey = attrKey.replace("__", ".");
				setAttr(attrKey,value);
				if (attrKey.startsWith("f_")){
					continue;
				}
				filter.add("and", sKey, op, value);
			}
			filter.end();
		}
		return filter;
	}
	
	protected void setAttrs(){
		setAttrs("");
	}
	
	protected void setAttrs(String prefix){

		setAttr(prefix+"limit", getPara("limit"));
		setAttr(prefix+"sort",getPara("sort"));
		setAttr(prefix+"order",getPara("order"));

		setAttr(prefix+"dlgFlag",getPara("dlgFlag"));
		
		setColField(prefix,listColumns);
	}	
	


	/**
	 * 返回列表显示字段的中文名
	 * @param s
	 * @return
	 */
	protected String[] getColNameList(String[][] s){
		String[] s1 = new String[s.length];
		for (int i = 0 ;i<s.length ;i++){
			s1[i] = s[i][1];
		}		
		return s1;
	}
	
	/**
	 * 返回列表显示字段
	 * @param s
	 * @return
	 */
	protected String[] getColFieldList(String[][] s){
		String[] s1 = new String[s.length];
		for (int i = 0 ;i<s.length ;i++){
			s1[i] = s[i][0];
		}		
		return s1;
	}
	
	/**
	 * 返回列表所显示的字段及其中文名
	 * @param s
	 * @return
	 */
	protected List<Map> setColField(String attrPrefix ,String[][] s){
		List<Map> list = new ArrayList<Map>();
		List<String> s0 = new ArrayList<String>();
		List<String> s1 = new ArrayList<String>();
		for (int i = 0 ;i<s.length ;i++){
			//标识为0的不显示到jsp中
			if (s[i].length > 2 && s[i][2].equals("0")){
				continue;
			}
			s0.add(s[i][0]);
			s1.add(s[i][1]);
			Map map = new HashMap();
			map.put("field", s[i][0]);
			map.put("fieldName", s[i][1]);
			list.add(map);
		}		
		setAttr(attrPrefix+"colFieldList", s0.toArray());
		setAttr(attrPrefix+"colNameList", s1.toArray());
		setAttr(attrPrefix+"colFieldMapList", list);
		return list;
	}	
	
	/**
	 * 返回列表所显示的字段及其中文名
	 * @param s
	 * @return
	 */
	protected List<Map> setColField(String[][] s){
		return setColField("",s);
	}
	
	public User getCurrUser(){
		return (User)super.getSession().getAttribute("currUser");
	}
	
}
